package com.qf.bookks.config;

import com.qf.bookks.filter.LogoutFilter;
import com.qf.bookks.interceptor.AutoLogonInterceptor;
import com.qf.bookks.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    AutoLogonInterceptor autoLogonInterceptor;
    @Autowired
    LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/plateform.html");
        registry.addInterceptor(autoLogonInterceptor)
                .addPathPatterns("/login.html");
    }

    @Bean
    public FilterRegistrationBean<LogoutFilter> logoutFilterFilterRegistrationBean(){
        FilterRegistrationBean<LogoutFilter> logoutFilterFilterRegistrationBean = new FilterRegistrationBean<>();
        final LogoutFilter filter = new LogoutFilter();
        filter.setName("logout");
        logoutFilterFilterRegistrationBean.addUrlPatterns("/logout");
        logoutFilterFilterRegistrationBean.setFilter(filter);
        return logoutFilterFilterRegistrationBean;
    }
}
